{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 批改标准 \n",
    "- 正确的log输出 60分。\n",
    "- 解释为什么这⾥的感知器代码⽆法完成异或功能 40分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],]\n",
    "\n",
    "\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],]\n",
    "\n",
    "\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perceptron(samples):\n",
    "    w = np.array([3, 5])\n",
    "    b = 1\n",
    "    a = 1 # lamda超参数\n",
    "\n",
    "    for i in range(10):\n",
    "        for j in range(len(samples )):\n",
    "            x = np.array(samples[j][:2])\n",
    "            y = 1 if np.dot(w, x) + b > 0 else 0\n",
    "            d = np.array(samples[j][2])\n",
    "\n",
    "            delta_b = a*(d-y) # b值的梯度\n",
    "            delta_w = a*(d-y)*x # w值的梯度\n",
    "\n",
    "            print('epoch {} sample {}  [{} {} {} {} {} {} {}]'.format(\n",
    "                i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b))\n",
    "            w = w + delta_w # 梯度下降\n",
    "            b = b + delta_b # 梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑与：\n",
      "epoch 0 sample 0  [3 5 1 1 0 0 -1]\n",
      "epoch 0 sample 1  [3 5 0 1 -1 0 -1]\n",
      "epoch 0 sample 2  [2 5 -1 1 0 -1 -1]\n",
      "epoch 0 sample 3  [2 4 -2 1 0 0 0]\n",
      "epoch 1 sample 0  [2 4 -2 0 0 0 0]\n",
      "epoch 1 sample 1  [2 4 -2 0 0 0 0]\n",
      "epoch 1 sample 2  [2 4 -2 1 0 -1 -1]\n",
      "epoch 1 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 2 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 2 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 2 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 2 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 3 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 3 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 3 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 3 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 4 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 4 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 4 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 4 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 5 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 5 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 5 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 5 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 6 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 6 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 6 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 6 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 7 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 7 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 7 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 7 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 8 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 8 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 8 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 8 sample 3  [2 3 -3 1 0 0 0]\n",
      "epoch 9 sample 0  [2 3 -3 0 0 0 0]\n",
      "epoch 9 sample 1  [2 3 -3 0 0 0 0]\n",
      "epoch 9 sample 2  [2 3 -3 0 0 0 0]\n",
      "epoch 9 sample 3  [2 3 -3 1 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "print('逻辑与：')\n",
    "perceptron(samples_and)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑或：\n",
      "epoch 0 sample 0  [3 5 1 1 0 0 -1]\n",
      "epoch 0 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 0 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 0 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 1 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 1 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 1 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 1 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 2 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 2 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 2 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 2 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 3 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 3 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 3 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 3 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 4 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 4 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 4 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 4 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 5 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 5 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 5 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 5 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 6 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 6 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 6 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 6 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 7 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 7 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 7 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 7 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 8 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 8 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 8 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 8 sample 3  [3 5 0 1 0 0 0]\n",
      "epoch 9 sample 0  [3 5 0 0 0 0 0]\n",
      "epoch 9 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 9 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 9 sample 3  [3 5 0 1 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "print('逻辑或：')\n",
    "perceptron(samples_or)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑异或：\n",
      "epoch 0 sample 0  [3 5 1 1 0 0 -1]\n",
      "epoch 0 sample 1  [3 5 0 1 0 0 0]\n",
      "epoch 0 sample 2  [3 5 0 1 0 0 0]\n",
      "epoch 0 sample 3  [3 5 0 1 -1 -1 -1]\n",
      "epoch 1 sample 0  [2 4 -1 0 0 0 0]\n",
      "epoch 1 sample 1  [2 4 -1 1 0 0 0]\n",
      "epoch 1 sample 2  [2 4 -1 1 0 0 0]\n",
      "epoch 1 sample 3  [2 4 -1 1 -1 -1 -1]\n",
      "epoch 2 sample 0  [1 3 -2 0 0 0 0]\n",
      "epoch 2 sample 1  [1 3 -2 0 1 0 1]\n",
      "epoch 2 sample 2  [2 3 -1 1 0 0 0]\n",
      "epoch 2 sample 3  [2 3 -1 1 -1 -1 -1]\n",
      "epoch 3 sample 0  [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 1  [1 2 -2 0 1 0 1]\n",
      "epoch 3 sample 2  [2 2 -1 1 0 0 0]\n",
      "epoch 3 sample 3  [2 2 -1 1 -1 -1 -1]\n",
      "epoch 4 sample 0  [1 1 -2 0 0 0 0]\n",
      "epoch 4 sample 1  [1 1 -2 0 1 0 1]\n",
      "epoch 4 sample 2  [2 1 -1 0 0 1 1]\n",
      "epoch 4 sample 3  [2 2 0 1 -1 -1 -1]\n",
      "epoch 5 sample 0  [1 1 -1 0 0 0 0]\n",
      "epoch 5 sample 1  [1 1 -1 0 1 0 1]\n",
      "epoch 5 sample 2  [2 1 0 1 0 0 0]\n",
      "epoch 5 sample 3  [2 1 0 1 -1 -1 -1]\n",
      "epoch 6 sample 0  [1 0 -1 0 0 0 0]\n",
      "epoch 6 sample 1  [1 0 -1 0 1 0 1]\n",
      "epoch 6 sample 2  [2 0 0 0 0 1 1]\n",
      "epoch 6 sample 3  [2 1 1 1 -1 -1 -1]\n",
      "epoch 7 sample 0  [1 0 0 0 0 0 0]\n",
      "epoch 7 sample 1  [1 0 0 1 0 0 0]\n",
      "epoch 7 sample 2  [1 0 0 0 0 1 1]\n",
      "epoch 7 sample 3  [1 1 1 1 -1 -1 -1]\n",
      "epoch 8 sample 0  [0 0 0 0 0 0 0]\n",
      "epoch 8 sample 1  [0 0 0 0 1 0 1]\n",
      "epoch 8 sample 2  [1 0 1 1 0 0 0]\n",
      "epoch 8 sample 3  [1 0 1 1 -1 -1 -1]\n",
      "epoch 9 sample 0  [0 -1 0 0 0 0 0]\n",
      "epoch 9 sample 1  [0 -1 0 0 1 0 1]\n",
      "epoch 9 sample 2  [1 -1 1 0 0 1 1]\n",
      "epoch 9 sample 3  [1 0 2 1 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "print('逻辑异或：')\n",
    "perceptron(samples_xor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 参数w和b无法确定，一直在更新"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"img/xor.jpg\", width=320, heigth=240>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<img src=\"img/xor.jpg\", width=320, heigth=240>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 与、或、非逻辑关系是线性的，单层感知机能分类线性问题，而异或逻辑是非线性问题，无法用单层感知机达到分类要求。如上面这副图，可以表示异或问题，无法用线性模型来分类这两种类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
